package com.wokeMy.view.util;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import cn.jpush.android.api.JPushInterface;

public class ViewTools {

	public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
                return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0,0);
                totalHeight += listItem.getMeasuredHeight();
        }
            
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
}
	public static void setListViewHeightBasedOnChildrenItem(ListView listView,int size) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
                return;
        }
        int totalHeight = 0;ViewGroup.LayoutParams params = listView.getLayoutParams();
        if (listAdapter.getCount()<size){
                for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
                        View listItem = listAdapter.getView(i, null, listView);
                        listItem.measure(0,0);
                        totalHeight += listItem.getMeasuredHeight();
                }
                params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        }else {
                for (int i = 0, len = 4; i < len; i++) {
                        View listItem = listAdapter.getView(i, null, listView);
                        listItem.measure(0,0);
                        totalHeight += listItem.getMeasuredHeight();
                }
                params.height = totalHeight + (listView.getDividerHeight() * (4 - 1));
        }
        listView.setLayoutParams(params);
}
        public static String getRegistrationID(Context context){
               return  JPushInterface.getRegistrationID(context);

        }

}
